﻿/*****************************************介绍*****************************************
 * 作者：24531
 * 创建时间：2025-09-07 18:47:39
 * 功能：界面【LevelWinSettlePanel】的View
        1、负责收集所有控件
**************************************************************************************/

using DG.Tweening;
using Game.Framework;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.LevelWinSettlePanel
{
    public class LevelWinSettlePanelView : PopupPanel
    {
        public const string PANEL_KEY = "Game_UI_LevelWinSettlePanel_LevelWinSettlePanel_Key";
        private RectTransform[] m_StarList;
        private int m_StarNum;      // 获得的星星数量
        private Sprite m_Bg;
        private GameDataModel.Level m_level;

        protected override void RegistAny()
        {
            PanelManager.Instance.RegistPanel(PANEL_KEY, this);
            EventCenter.Instance.AddEventListen<EAdType, ETriggerAdType>(GameEventName.ON_SHOW_SUCCESS_AD, ShowSuccessAd);
        }

        protected override void UnregistAny()
        {
            PanelManager.Instance.UnregistPanel(PANEL_KEY);
            EventCenter.Instance.RemoveEventListen<EAdType, ETriggerAdType>(GameEventName.ON_SHOW_SUCCESS_AD, ShowSuccessAd);
        }

        protected override void InitUI()
        {
            m_StarList = GetRect(LevelWinSettlePanelNotifications.Stars_RECT).GetComponentsInChildren<RectTransform>(true);
        }

        public override void Init(object data)
        {
            object[] datas = data as object[];
            m_level = datas[0] as GameDataModel.Level;
            m_StarNum = GameDataMgr.Instance.IsUnlockErrorLimit() ? Mathf.Clamp(m_level.lifeRemainNum, 1, 3) : 3;
            m_Bg = datas[1] as Sprite;
            DOTween.To(() => 0, x => GetControl<Text>(LevelWinSettlePanelNotifications.CoinNum_CTRL).text = $"+{x}", m_level.reward, 0.5f * (m_StarNum + 1));
            StartCoroutine(DelayShowStar());
        }

        private IEnumerator DelayShowStar()
        {
            AudioManager.Instance.PlayAudio("Audios/LevelPassed");
            yield return new WaitForSeconds(.5f);
            for (int i = 0; i < m_StarNum; i++)
            {
                m_StarList[i + 1].gameObject.SetActive(true);
                m_StarList[i + 1].DOScale(Vector3.one, 0.5f).SetEase(Ease.OutBack);
                if (i + 1 < m_StarList.Length - 1)
                    yield return new WaitForSeconds(0.6f);
            }
        }

        protected override void ClickBtnHandle(string name)
        {
            switch (name)
            {
                case LevelWinSettlePanelNotifications.ClaimBtn_CTRL:
                    NativeInteraction.Instance.ShowRewardAd(ETriggerAdType.LevelWinGetCoin);
                    break;
                case LevelWinSettlePanelNotifications.ContinueBtn_CTRL:
                    GameDataMgr.Instance.UpdateCoins(m_level.reward);
                    GameDataMgr.Instance.UpdateStar(m_StarNum);
                    if (m_level.level > 1)
                        UIUtility.DisplayPanel(NewJourenyPanel.NewJourenyPanelView.PANEL_KEY, "UI/NewJourenyPanel/Prefabs/NewJourenyPanel", data: new object[] { m_level, m_Bg });
                    else
                        EventCenter.Instance.Trigger(GameEventName.JumpMapTab);
                    UIUtility.HidePanel(PANEL_KEY);
                    break;
            }
        }

        private void ShowSuccessAd(EAdType adType, ETriggerAdType triggerAdType)
        {
            if (adType != EAdType.RewardedVideo || triggerAdType != ETriggerAdType.LevelWinGetCoin) return;
            // 发放奖励，这里是2倍金币
            GameDataMgr.Instance.UpdateCoins(m_level.reward * 2);
            GameDataMgr.Instance.UpdateStar(m_StarNum);
            if (m_level.level > 1)
                UIUtility.DisplayPanel(NewJourenyPanel.NewJourenyPanelView.PANEL_KEY, "UI/NewJourenyPanel/Prefabs/NewJourenyPanel", data: new object[] { m_level, m_Bg });
            else
                EventCenter.Instance.Trigger(GameEventName.JumpMapTab);
            UIUtility.HidePanel(PANEL_KEY);
        }

        private void OnDisable()
        {
            // 关闭星星
            for (int i = 1; i < m_StarList.Length; i++)
            {
                m_StarList[i].gameObject.SetActive(false);
                m_StarList[i].localScale = Vector3.one * 3;
            }
        }
    }
}